package day07.demo;
/**
 * 2.需求: 随机生成N位验证码(大写字母、小写字母、数字)
 *   训练目标:随机数+数组+方法
 */
import java.util.Random;

public class VerificationCode {
    public static void main(String[] args) {

        String num=generateVeriCode(4);
        System.out.println("验证码为:"+num);


    }
    /*
    public static String generateVeriCode(int n){
        String num="";//验证码
        Random rd=new Random();
        char[] chs={'a','b','c','d','e','f','g','h','i','j','k',
                'l','m','n','o','p','q','r','s','t','u','v',
                'w','x','y','z','A','B','C','D','E','F','G',
                'H','I','J','K','L','M','N','O','P','Q','R',
                'S','T','U','V','W','X','Y','Z','0','1','2',
                '3','4','5','6','7','8','9'};
        for (int i=0;i<n;i++){
            int index= rd.nextInt(chs.length);
            num+=chs[index];
        }
        return num;
    }

     */

    public static String generateVeriCode(int n){
        String num="";
        Random rd =new Random();
        char[] chs={'a','b','c','d','e','f','g','h','i','j','k',
                'l','m','n','o','p','q','r','s','t','u','v',
                'w','x','y','z','A','B','C','D','E','F','G',
                'H','I','J','K','L','M','N','O','P','Q','R',
                'S','T','U','V','W','X','Y','Z','0','1','2',
                '3','4','5','6','7','8','9'};
        for (int i=0;i<n;i++){
            int rdnum= rd.nextInt(62);
            num=num+chs[rdnum];
        }

        return num;
    }
}
